// $ANTLR 3.3 Nov 30, 2010 12:50:56 /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g 2012-03-05 13:34:45

import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;

public class CalcParser extends Parser {
    public static final String[] tokenNames = new String[] {
        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "NEWLINE", "Number", "WHITESPACE", "'+'", "'sin'", "'cos'", "'pi'", "'pow'", "'mult'", "'('", "')'"
    };
    public static final int EOF=-1;
    public static final int T__7=7;
    public static final int T__8=8;
    public static final int T__9=9;
    public static final int T__10=10;
    public static final int T__11=11;
    public static final int T__12=12;
    public static final int T__13=13;
    public static final int T__14=14;
    public static final int NEWLINE=4;
    public static final int Number=5;
    public static final int WHITESPACE=6;

    // delegates
    // delegators


        public CalcParser(TokenStream input) {
            this(input, new RecognizerSharedState());
        }
        public CalcParser(TokenStream input, RecognizerSharedState state) {
            super(input, state);
             
        }
        

    public String[] getTokenNames() { return CalcParser.tokenNames; }
    public String getGrammarFileName() { return "/home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g"; }



    // $ANTLR start "prog"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:3:1: prog : ( expr NEWLINE )+ EOF ;
    public final void prog() throws RecognitionException {
        double expr1 = 0.0;


        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:4:3: ( ( expr NEWLINE )+ EOF )
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:4:5: ( expr NEWLINE )+ EOF
            {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:4:5: ( expr NEWLINE )+
            int cnt1=0;
            loop1:
            do {
                int alt1=2;
                int LA1_0 = input.LA(1);

                if ( (LA1_0==Number||(LA1_0>=8 && LA1_0<=13)) ) {
                    alt1=1;
                }


                switch (alt1) {
            	case 1 :
            	    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:4:6: expr NEWLINE
            	    {
            	    pushFollow(FOLLOW_expr_in_prog13);
            	    expr1=expr();

            	    state._fsp--;

            	    match(input,NEWLINE,FOLLOW_NEWLINE_in_prog15); 
            	    System.out.println("=" + expr1);

            	    }
            	    break;

            	default :
            	    if ( cnt1 >= 1 ) break loop1;
                        EarlyExitException eee =
                            new EarlyExitException(1, input);
                        throw eee;
                }
                cnt1++;
            } while (true);

            match(input,EOF,FOLLOW_EOF_in_prog21); 

            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "prog"


    // $ANTLR start "expr"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:7:1: expr returns [double value] : ( term ( '+' T= expr )? | 'sin' T= expr | 'cos' T= expr | 'pi' | 'pow' X= expr Y= expr | 'mult' X= term Y= term );
    public final double expr() throws RecognitionException {
        double value = 0.0;

        double T = 0.0;

        double X = 0.0;

        double Y = 0.0;

        double term2 = 0.0;


        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:8:3: ( term ( '+' T= expr )? | 'sin' T= expr | 'cos' T= expr | 'pi' | 'pow' X= expr Y= expr | 'mult' X= term Y= term )
            int alt3=6;
            switch ( input.LA(1) ) {
            case Number:
            case 13:
                {
                alt3=1;
                }
                break;
            case 8:
                {
                alt3=2;
                }
                break;
            case 9:
                {
                alt3=3;
                }
                break;
            case 10:
                {
                alt3=4;
                }
                break;
            case 11:
                {
                alt3=5;
                }
                break;
            case 12:
                {
                alt3=6;
                }
                break;
            default:
                NoViableAltException nvae =
                    new NoViableAltException("", 3, 0, input);

                throw nvae;
            }

            switch (alt3) {
                case 1 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:8:5: term ( '+' T= expr )?
                    {
                    pushFollow(FOLLOW_term_in_expr40);
                    term2=term();

                    state._fsp--;

                    value += term2;
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:9:5: ( '+' T= expr )?
                    int alt2=2;
                    int LA2_0 = input.LA(1);

                    if ( (LA2_0==7) ) {
                        alt2=1;
                    }
                    switch (alt2) {
                        case 1 :
                            // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:9:6: '+' T= expr
                            {
                            match(input,7,FOLLOW_7_in_expr50); 
                            pushFollow(FOLLOW_expr_in_expr54);
                            T=expr();

                            state._fsp--;

                            value += T;

                            }
                            break;

                    }


                    }
                    break;
                case 2 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:10:5: 'sin' T= expr
                    {
                    match(input,8,FOLLOW_8_in_expr64); 
                    pushFollow(FOLLOW_expr_in_expr68);
                    T=expr();

                    state._fsp--;

                    value = Math.sin(T);

                    }
                    break;
                case 3 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:11:5: 'cos' T= expr
                    {
                    match(input,9,FOLLOW_9_in_expr76); 
                    pushFollow(FOLLOW_expr_in_expr80);
                    T=expr();

                    state._fsp--;

                    value = Math.cos(T);

                    }
                    break;
                case 4 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:12:5: 'pi'
                    {
                    match(input,10,FOLLOW_10_in_expr88); 
                    value = Math.PI;

                    }
                    break;
                case 5 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:13:5: 'pow' X= expr Y= expr
                    {
                    match(input,11,FOLLOW_11_in_expr97); 
                    pushFollow(FOLLOW_expr_in_expr101);
                    X=expr();

                    state._fsp--;

                    pushFollow(FOLLOW_expr_in_expr105);
                    Y=expr();

                    state._fsp--;

                    value = Math.pow(X, Y);

                    }
                    break;
                case 6 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:14:5: 'mult' X= term Y= term
                    {
                    match(input,12,FOLLOW_12_in_expr113); 
                    value =1;
                    pushFollow(FOLLOW_term_in_expr119);
                    X=term();

                    state._fsp--;

                    pushFollow(FOLLOW_term_in_expr123);
                    Y=term();

                    state._fsp--;

                    value = X * Y;

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "expr"


    // $ANTLR start "term"
    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:17:1: term returns [double value] : ( Number | '(' expr ')' );
    public final double term() throws RecognitionException {
        double value = 0.0;

        Token Number3=null;
        double expr4 = 0.0;


        try {
            // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:18:3: ( Number | '(' expr ')' )
            int alt4=2;
            int LA4_0 = input.LA(1);

            if ( (LA4_0==Number) ) {
                alt4=1;
            }
            else if ( (LA4_0==13) ) {
                alt4=2;
            }
            else {
                NoViableAltException nvae =
                    new NoViableAltException("", 4, 0, input);

                throw nvae;
            }
            switch (alt4) {
                case 1 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:18:5: Number
                    {
                    Number3=(Token)match(input,Number,FOLLOW_Number_in_term144); 

                          value = Double.parseDouble((Number3!=null?Number3.getText():null));
                        

                    }
                    break;
                case 2 :
                    // /home/ubuntu/kenpu-teaching/201201/csci4020u/03-antlr-calculator/src/Calc.g:22:5: '(' expr ')'
                    {
                    match(input,13,FOLLOW_13_in_term156); 
                    pushFollow(FOLLOW_expr_in_term158);
                    expr4=expr();

                    state._fsp--;

                    match(input,14,FOLLOW_14_in_term160); 

                          value = expr4;
                        

                    }
                    break;

            }
        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return value;
    }
    // $ANTLR end "term"

    // Delegated rules


 

    public static final BitSet FOLLOW_expr_in_prog13 = new BitSet(new long[]{0x0000000000000010L});
    public static final BitSet FOLLOW_NEWLINE_in_prog15 = new BitSet(new long[]{0x0000000000003F20L});
    public static final BitSet FOLLOW_EOF_in_prog21 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_term_in_expr40 = new BitSet(new long[]{0x0000000000000082L});
    public static final BitSet FOLLOW_7_in_expr50 = new BitSet(new long[]{0x0000000000003F20L});
    public static final BitSet FOLLOW_expr_in_expr54 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_8_in_expr64 = new BitSet(new long[]{0x0000000000003F20L});
    public static final BitSet FOLLOW_expr_in_expr68 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_9_in_expr76 = new BitSet(new long[]{0x0000000000003F20L});
    public static final BitSet FOLLOW_expr_in_expr80 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_10_in_expr88 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_11_in_expr97 = new BitSet(new long[]{0x0000000000003F20L});
    public static final BitSet FOLLOW_expr_in_expr101 = new BitSet(new long[]{0x0000000000003F20L});
    public static final BitSet FOLLOW_expr_in_expr105 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_12_in_expr113 = new BitSet(new long[]{0x0000000000002020L});
    public static final BitSet FOLLOW_term_in_expr119 = new BitSet(new long[]{0x0000000000002020L});
    public static final BitSet FOLLOW_term_in_expr123 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_Number_in_term144 = new BitSet(new long[]{0x0000000000000002L});
    public static final BitSet FOLLOW_13_in_term156 = new BitSet(new long[]{0x0000000000003F20L});
    public static final BitSet FOLLOW_expr_in_term158 = new BitSet(new long[]{0x0000000000004000L});
    public static final BitSet FOLLOW_14_in_term160 = new BitSet(new long[]{0x0000000000000002L});

}